package edu.bw.controller;

import edu.bw.common.CommonResult;
import edu.bw.dto.SelectDeptByConditionForm;
import edu.bw.dto.UpdateDeptForm;
import edu.bw.po.Dept;
import edu.bw.service.DeptService;
import edu.bw.utils.PageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/dept")
@CrossOrigin
public class DeptController {
    @Autowired
    private DeptService deptService;

    @PostMapping("/selectDeptByCondition")
    public CommonResult selectDeptByCondition(@RequestBody SelectDeptByConditionForm selectConditionForm) {
        PageUtils pageUtils = deptService.selectDeptByCondition(selectConditionForm);
        return CommonResult.ok().put("page", pageUtils);
    }

    @PostMapping("/insert")
    public CommonResult insertDept(@RequestBody Dept dept) {
        Integer rows = deptService.insert(dept);
        return CommonResult.ok().put("rows", rows);
    }

    @PostMapping("/selectById")
    public CommonResult selectById(@RequestBody Dept dept) {
        List<Map<String, Object>> list = deptService.selectById(dept.getId());
        return CommonResult.ok(list.get(0));
    }

    @PostMapping("/update")
    public CommonResult updateDept(@RequestBody UpdateDeptForm updateDeptForm) {
        Integer rows = deptService.update(updateDeptForm);
        return CommonResult.ok().put("rows", rows);
    }

    @PostMapping("/deleteDeptByIds")
    public CommonResult deleteDeptByIds(@RequestBody Map<String, List<Integer>> params) {
        List<Integer> ids = params.get("ids");
        Integer rows = deptService.deleteDeptByIds(ids);
        return CommonResult.ok().put("rows", rows);
    }

    @GetMapping("/selectAllDept")//前端用的get请求 所以这里用get
    public CommonResult selectAllDept(){
        List<Map<String, Object>> list = deptService.selectAllDept();
        return CommonResult.ok().put("list", list);
    }
}
